package com.inews.business.web;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inews.base.mapper.JsonMapper;
import com.inews.base.utils.InewsUtils;
import com.inews.base.utils.Servlets;
import com.inews.base.vo.ExtData;
import com.inews.business.service.SysUserService;
import com.inews.business.vo.SysRoleVo;
import com.inews.business.vo.SysUserVo;

// TODO: Auto-generated Javadoc
/**
 * The Class SysUserController.
 */
@Controller
@RequestMapping("/sysUser")
public class SysUserController {
	
	@Autowired
	private SysUserService sysUserService;
	
	
	/**
	 * 分页查询系统用户信息.
	 *
	 * @param request the request
	 * @param page the page
	 * @return the object
	 */
	@RequestMapping("findSysUserPage")
	@ResponseBody
	public Object findSysUserPage(ServletRequest request, @RequestParam(value = "page", defaultValue = "1") int pageNumber, String sort){
		Pageable page = Servlets.buildPageRequest(pageNumber, sort);
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		ExtData<SysUserVo> extData = sysUserService.findSysUserPage(searchParams, page);
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	
	
	
	
	/**
	 * 保存用户.
	 *
	 * @param vo the vo
	 * @return the object
	 * @throws Exception 
	 */
	@RequestMapping("updateUserPassword")
	@ResponseBody
	public Object updateUserPassword(SysUserVo vo){
		ExtData<SysUserVo> extData = sysUserService.doResetPassword(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	/**
	 * 保存用户.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("saveSysUser")
	@ResponseBody
	public Object saveSysUser(SysUserVo vo){
		ExtData<SysUserVo> extData = sysUserService.saveSysUser(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 更新用户.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("updateSysUser")
	@ResponseBody
	public Object updateSysUser(SysUserVo vo){
		ExtData<SysUserVo> extData = sysUserService.updateSysUser(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 删除用户.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("deleteSysUser")
	@ResponseBody
	public Object deleteSysUser(SysUserVo vo){
		ExtData<SysUserVo> extData = sysUserService.deleteSysUser(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	
	/**
	 * 根据用户Id查询对应的角色信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("findRoleByUser")
	@ResponseBody
	public Object findRoleByUser(SysUserVo vo){
		ExtData<SysRoleVo> extData = sysUserService.findRoleByUser(vo);
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	
	/**
	 * 分配用户角色.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("chooseRole")
	@ResponseBody
	public Object chooseRole(SysUserVo vo, String roles){
		List<SysRoleVo> roleVos = JsonMapper.nonDefaultMapper().fromJson(roles, JsonMapper.nonDefaultMapper().createCollectionType(List.class, SysRoleVo.class));
		ExtData<SysUserVo> extData = sysUserService.chooseRole(vo, roleVos, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	

}
